
Imports cmplib01


Public Class frmDeployment

    Private _deployCancelled As Boolean

    Private ptrBaseBG As BattlegroupChit
    Private ptrGame As CampaignerGameData
    Private ptrdepZone As DeploymentZone
    Private _OOBDeploy As BattlegroupChit
    Private _OOBGroupDeployed As Boolean

    Public Overloads Sub ShowDialog(ByRef game As CampaignerGameData, ByRef depZone As DeploymentZone, ByRef BG As BattlegroupChit, ByVal moveTypes As String())

        ptrBaseBG = BG
        ptrGame = game
        ptrdepZone = depZone

        cgGroups.forceOOB = game.ForceOOB
        cgGroups.moveTypeNames = moveTypes
        cgGroups.Game = ptrGame
        cgGroups.BaseBattleGroup = ptrBaseBG
        cgGroups.DeploymentZone = ptrdepZone

        MyBase.ShowDialog()
    End Sub

    Public ReadOnly Property OOBDeploy() As BattlegroupChit
        Get
            Return _OOBDeploy
        End Get
    End Property

    Public ReadOnly Property OOBGroupDeployed() As Boolean
        Get
            Return _OOBGroupDeployed
        End Get
    End Property

    Public Property defaultBGIcon() As Bitmap
        Get
            Return cgGroups.defaultBGIcon
        End Get
        Set(ByVal value As Bitmap)
            cgGroups.defaultBGIcon = value
        End Set
    End Property

    Public ReadOnly Property DeployCancelled() As Boolean
        Get
            Return _deployCancelled
        End Get
    End Property


    Private Sub btnDeploy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeploy.Click
        If cgGroups.IsValid = False Then
            _deployCancelled = True
            Exit Sub
        Else
            _deployCancelled = False
        End If

        Me.Hide()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        _deployCancelled = True

        '// make sure any OOBgroups are put back where they belong
        RestoreOOBGroups()

        Me.Hide()

    End Sub

    Private Sub RestoreOOBGroups()
        Try
            For Each chitmem As ChitMember In ptrBaseBG.members
                If chitmem.UnitType = GroundChitClass_cgd.Battlegroup Then
                    If DirectCast(chitmem, BattlegroupChit).OOBGroup = True Then
                        ptrGame.selectedFaction.OrderOfBattle.add(chitmem)
                    End If
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cgGroups_DeployOOBAsBG(ByRef OOB As BattlegroupChit) Handles cgGroups.DeployOOBAsBG
        Try
            '// remove the OOB from the owning faction, there is no cancel after pressing "Deploy", check for point value violations as well.
            _OOBDeploy = OOB
            '// overwrite the normal base BG
            cgGroups.BaseBattleGroup = OOB
            ptrBaseBG = OOB
            '// remove him from the list
            ptrGame.selectedFaction.OrderOfBattle.remove(OOB)
            '// set up PV
            ptrBaseBG.deployedpointvalue = ptrBaseBG.SuggestedPointValue
            _OOBGroupDeployed = True
            ptrBaseBG.OOBGroup = True
            ptrBaseBG.OOBUnit = True

            btnDeploy_Click(Nothing, Nothing)
        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub

    Private Sub btnDeploySingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeploySingle.Click
        '// Allows the player to deploy a single unit. Jam.

        Try
            Dim funit As New frmSelectSingleUnit 'frmUnitSelection()
            funit.showDialog(ptrGame, ptrdepZone, cgGroups.moveTypeNames)

            '// do a happy dance.


        Catch ex As Exception
            HandleException(c_Module, ex)
        End Try
    End Sub
End Class